package org.example.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.JavaBean.Emp;
import org.example.mapper.Empmapper;
import org.example.service.EmpService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Auther:Liu
 * @Testname:EmpServiceImpl
 * @Date:2025/7/31 11:11
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private Empmapper empmapper;
    @Override
    public JsonResult findAll(int page, int size) {
       PageHelper.startPage(page, size);
       List<Emp> list = empmapper.findAll();
       PageInfo<Emp> pageInfo = new PageInfo<>(list);
       return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult findAllJob() {
        return ResultTool.success(empmapper.findAllJob());
    }

    @Override
    public JsonResult findAllMgr() {
        return ResultTool.success(empmapper.findAllMgr());
    }

    @Override
    public JsonResult findEnameCount(String ename) {
       int count = empmapper.findEnameCount(ename);
       if (count > 0) {
           return ResultTool.fail(500,"姓名已存在");
       }else{
           return ResultTool.success("姓名可使用");
       }
    }

    @Override
    public JsonResult save(Emp emp) {
        int save = empmapper.save(emp);
        if (save== 0) {
            return ResultTool.fail(500,"添加员工失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult removeById(int id) {
        int i = empmapper.removeById(id);
        if (i ==0) {
            return ResultTool.fail(500,"删除失败");
        }
            return ResultTool.success("success");

    }

    @Override
    public JsonResult removeByBatch(String ids) {
        String[] idArr = ids.split(",");
        List<String> list = Arrays.asList(idArr);
        int i = empmapper.removeByBatch(list);
        if (i ==0) {
            return ResultTool.fail(500,"批量删除失败");
        }
            return ResultTool.success("success");

    }

    @Override
    public JsonResult update(Emp emp) {
        int i = empmapper.update(emp);
        if (i ==0) {
            return ResultTool.fail(500,"数据更新失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findCascade(Emp emp, int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> list = empmapper.findCascade(emp);
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }
}
